﻿// --------------------------------
// <copyright file="SerializerForm.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>4. 12. 2011 13:55:16</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.IO;

namespace FITClub.LevelBuilder
{
    public partial class SerializerForm : Form
    {
        public SerializerForm()
        {
            InitializeComponent();
        }

        private void SerializerForm_Load(object sender, EventArgs e)
        {
            Assembly asm = Assembly.GetAssembly(typeof(FITClub.Episode1.GameFITClub));
            var types = from tp in asm.GetTypes() where tp.IsClass && tp.IsAbstract == false && tp.IsInterface == false && tp.IsSubclassOf(typeof(GameComponent)) == false orderby tp.Name select tp;
            List<Type> datasource = types.ToList();

            var types2 = from tp in Assembly.GetExecutingAssembly().GetTypes() where tp.BaseType == typeof(object) select tp;
            datasource.AddRange(types2);
            this.comboBox1.DataSource = datasource;
            this.comboBox1.DisplayMember = "Name";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Type selectedType;
            XmlSerializer xser;
            try
            {
                selectedType = this.comboBox1.SelectedItem as Type;
                xser = new XmlSerializer(selectedType);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return;
            }           
            using (MemoryStream ms = new MemoryStream(10240))
            {
                try
                {
                    ConstructorInfo ci = selectedType.GetConstructor(Type.EmptyTypes);
                    xser.Serialize(ms, ci.Invoke(null));
                    ms.Seek(0, SeekOrigin.Begin);
                    this.textBox1.Text = Encoding.UTF8.GetString(ms.ToArray());
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Unable to serialize "+selectedType.Name);
                }
            }
        }


    }
}
